{extend name="public/container"}
{block name="content"}
<div id="myApp">
    <div class="douxue-filter padding-10 mb-20 flex flex-between" style="margin-bottom:20px">
        <el-form :inline="true" :model="where" class="flex">
            <div class="flex flex-between">
                <el-form-item>
                    <el-select size="small"  v-model="where.status" placeholder="请选择" :clearable=true>
                        <el-option label="显示" :value="1"></el-option>
                        <el-option label="不显示" :value="0"></el-option>
                    </el-select>
                </el-form-item>

                <el-form-item>
                    <el-input size="small" v-model="where.role_name" placeholder="请输入身份昵称">
                        <el-button slot="append" size="small" type="primary" @click="doLoadRows(1)" icon="el-icon-search"></el-button>
                    </el-input>
                </el-form-item>
            </div>
        </el-form>

        <el-button type="primary" size="mini" @click="doAdd()">新增身份</el-button>
    </div>
    <div class="padding-10">
        <el-table
                border
                stripe
                :data="rows"
                style="width: 100%"
                size="mini">
            <el-table-column
                    prop="id"
                    label="编号"
                    width="200"
                    align="left">
            </el-table-column>
            <el-table-column
                    prop="role_name"
                    label="身份昵称"
                    width="160"
                    align="left">
            </el-table-column>
            <el-table-column
                    prop="rule_lists"
                    label="权限"
                    align="left">
            </el-table-column>
            <el-table-column
                    label=""
                    width="90"
                    align="left">
                <template slot-scope="scope">
                    <el-button size="mini" type="primary" @click="doAdd(scope.row)" icon="el-icon-edit"></el-button>
                    <el-button size="mini" type="danger" @click="doDelete(scope.row.id)" icon="el-icon-delete"></el-button>
                </template>
            </el-table-column>
        </el-table>
        <div class="pagination">
            <el-pagination
                    hide-on-single-page
                    small
                    layout="prev, pager, next"
                    :total="rowsCount"
                    :page-size="where.limit"
                    @current-change="doLoadRows">
            </el-pagination>
        </div>
    </div>
    <!-- 添加/编辑   -->
    <el-dialog title="添加/编辑" :visible.sync="dialogFormVisible" v-if="dialogFormVisible" @close='closeDialog'>
        <el-form :model="formData" :rules="rules" ref="formData" label-width="100px" >
            <el-form-item label="身份名称" prop="role_name">
                <el-input v-model="formData.role_name" placeholder="身份名称" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="状态" prop="status">
                <el-radio v-model="formData.status" label="1">是</el-radio>
                <el-radio v-model="formData.status" label="0">否</el-radio>
            </el-form-item>

            <el-form-item label='权限'>
                <el-tree :data="menus" ref="tree" @check-change="getCheckId" show-checkbox node-key="id" :default-checked-keys="checked_menus" :props="defaultProps"></el-tree>
            </el-form-item>

            <el-form-item>
                <el-button type="primary" @click="onSubmit('formData')">确 定</el-button>
                <el-button @click="dialogFormVisible = false">取消</el-button>
            </el-form-item>
        </el-form>
    </el-dialog>
</div>
{/block}
{block name="script"}
<script>
    var menus = <?=isset($menus) ? json_encode($menus) : "[]"?>;
    var my = new Vue({
        el: "#myApp",
        data() {
            return{
                dialogFormVisible: false,
                formData:{
                    id:0,
                    role_name: '',
                    status: '1'
                },
                checked_menus: [],
                rules:{
                    role_name: [
                        { required: true, message: '管理员账号不可以为空', trigger: 'blur' },
                    ],
                },

                rowsCount: 0,
                rows: [],
                where:{
                    page:1,
                    limit: pageSize,
                    role_name: '',
                    status: '',
                },
                menus: menus,
                defaultProps: {
                    children: 'child',
                    label: 'menu_name'
                }
            }
        },
        methods: {
            doLoadRows (pn) {
                this.where.page = pn || 1;
                console.log(pn)
                axios.get(Url({a:'get_list' , q:this.where})).then(res => {
                    this.rows = res.data.data
                    this.rowsCount = res.data.count
                })
            }
            // 编辑
            ,doAdd ( item ) {
                if ( item != undefined ) {
                    item.status += '';
                    this.formData = item;
                    this.checked_menus = item.rules;
                } else {
                    this.formData = {
                        id:0,
                        role_name: '',
                        status: '1'
                    }
                    this.checked_menus = [];
                }
                this.dialogFormVisible = true
            }
            ,onSubmit (formName) {
                console.log(this.$refs.tree.getCheckedKeys());
                this.formData.rules = this.$refs.tree.getCheckedKeys();
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        axios.post( Url({a:'save'}) , this.formData)
                            .then(res => {
                                if(res.data.code == 200){
                                    this.$message({
                                        message:'操作成功',
                                        type:'success',
                                        duration:1500
                                    });
                                }else{
                                    this.$message({
                                        message:res.data.msg,
                                        type:'error',
                                        duration:1500
                                    });
                                }

                                this.dialogFormVisible = false;
                                this.doLoadRows(1)
                            }).catch(_ => {
                            this.$message({
                                message: '操作失败',
                                type: 'error'
                            })
                        })
                    } else {
                        console.log('error submit!!');
                        return false;
                    }
                });
            }
            ,doDelete (id) {
                this.$confirm('确认删除该条记录?').then(_ => {
                    axios.get(Url({a:'delete' , q:{id:id}})).then(res => {
                        this.$message({
                            message: res.data.msg,
                            type: res.data.code === 200 ? 'success' : 'error',
                        })
                    }).catch(_ => {
                        this.$message({
                            message: '删除失败',
                            type: 'error'
                        })
                    })

                    return this.doLoadRows();
                }).catch(_ => {})
            }
            // 关闭
            ,closeDialog () {
                this.$refs['formData'].clearValidate();
            }
            ,getCheckId (menus) {
                var data = [];

            }
            ,initMenu(menu){

            }
        },
        mounted(){
            this.doLoadRows()
        }
    })
</script>
{/block}